﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KXmlUIControl.Properties;

namespace KXmlUIControl.KControl
{
    public partial class KCheckBox : UserControl, INotifyPropertyChanged
    {
        private Color _titleColor = Color.Black;
        public Color TitleColor
        {
            get
            {
                return _titleColor;
            }
            set
            {
                if (value != _titleColor)
                {
                    _titleColor = value;
                    labelTitle.ForeColor = _titleColor;
                    RaiseDegreeChanged("TitleColor");
                }
            }
        }

        private bool _checked = false;
        public bool Checked
        {
            get
            {
                return _checked;
            }
            set
            {
                if (_checked != value)
                {
                    _checked = value;
                    pictureBoxIcon.Image = (_checked ? Resources.Checked : Resources.Unchecked);
                    RaiseDegreeChanged("Checked");
                    OnCheckedChanged();
                }
            }
        }

        private string _title = "KCheckBox";
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    labelTitle.Text = value;
                    ResetSize();
                    RaiseDegreeChanged("Title");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaiseDegreeChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaiseDegreeChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event EventHandler CheckedChanged;
        protected void OnCheckedChanged()
        {
            if (CheckedChanged != null)
            {
                CheckedChanged(this, null);
            }
        }

        public KCheckBox()
        {
            InitializeComponent();
        }

        private void ResetSize()
        {
            labelTitle.Left = pictureBoxIcon.Right + 2;
            this.Width = pictureBoxIcon.Width + labelTitle.Width + 5;
        }

        private void pictureBoxIcon_Click(object sender, EventArgs e)
        {
            Checked = !_checked;
        }

        private void labelTitle_Click(object sender, EventArgs e)
        {
            Checked = !_checked;
        }

    }
}
